package com.ruoyi.project.module.Job;

import com.crawler.XxlCrawler;
import com.crawler.annotation.PageFieldSelect;
import com.crawler.annotation.PageSelect;
import com.crawler.conf.XxlCrawlerConf;
import com.crawler.parser.PageParser;
import com.ruoyi.project.module.category.domain.Category;
import com.ruoyi.project.module.category.service.ICategoryService;
import com.ruoyi.project.module.item.service.IItemService;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.framework.aspectj.lang.constant.CategoryType.BUYER_CITY_SELECTED_DB;
import static com.ruoyi.framework.aspectj.lang.constant.CategoryType.CACHE_CONTROL;

//@Component("CrawlItem")
public class CrawlItem {

    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IItemService itemService;

    private XxlCrawler xxlCrawlerStop;

    @Data
    @PageSelect(cssQuery = "body")
    public static class CrawlItemPageVo {

        //爬取商品的名称
        @PageFieldSelect(cssQuery = "h6.itemTitle", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> itemNameList_h6_itemTitle;

        //爬取商品的链接
        @PageFieldSelect(cssQuery = "a.itemLink.block", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "href")
        private List<String> itemHrefList_a_itemLink_block;

        //爬取商品的价格
        @PageFieldSelect(cssQuery = "span.itemPrice", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> itemPriceList_span_itemPrice;

        //爬第一种位置的EAN
        @PageFieldSelect(cssQuery = "a.img-link", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "data-ean")
        private List<String> eanList_a_img_link;

        //爬第二种位置的EAN
        @PageFieldSelect(cssQuery = "div.column-block", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "data-ean")
        private List<String> eanList_div_column_block;

        //结束标志
        @PageFieldSelect(cssQuery = ".warning > .clearMargin-bottom > li", selectType = XxlCrawlerConf.SelectType.TEXT)
        private String endState_warning_clearMargin_bottom_li;


    }

    public void CrawlItemStop(String name) {
        xxlCrawlerStop.tryFinish();
        System.out.println("停止！！"+name);
    }


    public void crawlItem(String name) {

        System.out.println("name:"+name);

        Map headerMap = new HashMap();
        Map cookieMap = new HashMap();
        List<CrawlItemPageVo> crawlItemPageVoList = new ArrayList<>();

        cookieMap.put(BUYER_CITY_SELECTED_DB.getDesc(), BUYER_CITY_SELECTED_DB.getCode());
        headerMap.put(CACHE_CONTROL.getDesc(), CACHE_CONTROL.getCode());

        //查询需要爬取的类目信息
        List<Category> categoriesList = categoryService.selectCategoryList(null);

        for (Category categories : categoriesList) {

            final String[] link = new String[500];

            //如果是垃圾数据则跳过
            if ("null".equals(categories.getCategoryHref())) {
                continue;
            }
            //生成待爬连接池
            for (int j = 1; j <= categories.getMaxpage(); j++) {
                link[j] = categories.getCategoryHref().replace("?ref=nav", "") + "&section=2&page=" + j;
            }
            //查询一个类目下的所有页商品
            XxlCrawler crawler = new XxlCrawler.Builder()
                    .setUrls(link)
                    .setThreadCount(1).setPauseMillis(1000)
                    .setAllowSpread(false)
                    .setFailRetryCount(3)
                    //.setProxyMaker(proxyMaker)
                    .setCookieMap(cookieMap)
                    .setHeaderMap(headerMap)
                    .setPageParser(
                            new PageParser<CrawlItemPageVo>() {
                                @Override
                                public boolean parse(Document html, Element pageVoElement, CrawlItemPageVo crawlItemPageVo) {
                                    crawlItemPageVoList.add(crawlItemPageVo);
                                    //分页结束标志
                                    return crawlItemPageVo.getEndState_warning_clearMargin_bottom_li() == null;
                                }
                            }
                    )
                    .build();
            xxlCrawlerStop = crawler;
            crawler.start(true);
            //把商品插入数据库
            itemService.insertItemAsCrawlItem(crawlItemPageVoList);


            //用于停止循环
            if (!crawler.isRunState()) {
                System.out.println("-----------------这个类别已结束-------------------------");
                break;
            }
        }
        xxlCrawlerStop.stop();

    }
}
